﻿<%@ WebHandler Language="C#" Class="Rights" %>

using System;
using System.Web;

public class Rights : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
    Spring.Context.IApplicationContext appContext = Spring.Context.Support.ContextRegistry.GetContext();
    GONES.Tools.Common.LoginInfo loginInfo = new GONES.Tools.Common.LoginInfo();
    public string ErrMsg { get; set; }
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
       string title = context.Request["title"];
        if (context.Session["LoginInfo"] != null)
        {
            loginInfo = (GONES.Tools.Common.LoginInfo)context.Session["LoginInfo"];
        }
        if (string.IsNullOrEmpty(title))
        {
            failure("无指定操作");
        }
        else
        {
            switch (title)
            {
                case "SetPermision":
                    if(SetPermision(context.Request["roleId"], context.Request["navPermissionId"]))
                    {
                        context.Response.Write(success("", "RightsSet"));
                    }
                    else
                    {
                        context.Response.Write(failure(ErrMsg));
                    }
                    break;
            }
        }
    }

    public bool SetPermision(string roleid, string navpermissionidstr)
    {
        if (navpermissionidstr == "1") //未做任何修改
            return true;
        navpermissionidstr = navpermissionidstr.Substring(0, navpermissionidstr.Length - 1);
        var rolepermissionManager = appContext.GetObject("S_RolePermissionManager") as GONES.IBLL.IS_RolePermissionManager;
        GONES.Model.S_RolePermission rolepermission = null;
        string[] rolepermissionidList = navpermissionidstr.Split('|');
        try
        {
            System.Collections.Generic.IList<GONES.Model.S_RolePermission> roleperlists = rolepermissionManager.GetRolePermissionList(roleid);
            foreach (var rp in roleperlists)
            {
                rolepermissionManager.Delete(rp.ID);   
            }            
            for (int i = 0; i < rolepermissionidList.Length; i++)
            {
                rolepermission = new GONES.Model.S_RolePermission();
                rolepermission.ID = GONES.Tools.Common.Rnd.Guid;
                rolepermission.RoleId = roleid;
                rolepermission.NavPermissionid = rolepermissionidList[i];
                rolepermissionManager.Add(rolepermission);
            }
            return true;
        }
        catch
        {
            return false; 
        }
         
    }
    
    public string success()
    {
        return "{\"statusCode\":\"200\", \"message\":\"操作成功!\"}";
    }
    public string success(string str, string navTabId)
    {
        return "{\"statusCode\":\"200\", \"message\":\"操作成功!" + str + "\",\"navTabId\":\"" + navTabId + "\"}";
    }
    public string failure()
    {
        return "{\"statusCode\":\"300\", \"message\":\"操作失败\"}";
    }
    public string failure(string str)
    {
        return "{\"statusCode\":\"300\", \"message\":\"操作失败!" + str + "\"}";
    }
    
    public bool IsReusable {
        get {
            return false;
        }
    }

}